package com.zcyr.controller;

import com.zcyr.pojo.UserInfo;
import com.zcyr.util.JsonBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@CrossOrigin(allowCredentials = "true")
@RestController
@RequestMapping("user")
public class UserController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    HttpServletRequest request;

    @RequestMapping("info")
    public JsonBody getUserInfo(){
        HttpSession session = request.getSession(false);
        return JsonBody.ok().put("userInfo", session.getAttribute("userInfo"));
    }

    @RequestMapping("logout")
    public JsonBody logout(){
        HttpSession session =  request.getSession(false);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        logger.info(String.format("用户(%s)退出了登录!", userInfo.getName()));
        session.invalidate();
        return JsonBody.ok("退出登录成功!");
    }
}
